package top.wbhzx.wangpan.util;

import org.joda.time.DateTime;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author JiangZuoWei
 * @date 2018年4月26日 下午5:00:31
 * @remark
 */
public class DateUtils {

	public static final String DATETIME_YYYYMMDD_HHMMSS = "yyyy年MM月dd日 HH:mm:ss";

	public static final String DATETIME = "yyyy-MM-dd";

	public static final String TIME_HHMMSS = "HH:mm:ss";
	
	public static final String DATATIME_FORMAT_YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";

	public static final String DATATIME_FORMAT_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

	public static final String DATATIME_FORMAT = "yyyyMMdd HH:mm:ss";

	public static final String DATEFORDAY = "yyyyMMdd";

	public static String getCurrentTime() {
		return DateTime.now().toString(DATATIME_FORMAT_YYYYMMDDHHMMSSSSS);
	}

	public static String getNowTime() {
		return DateTime.now().toString(DATETIME_YYYYMMDD_HHMMSS);
	}

	public static Long getCurrentTimes() {
		return Long.parseLong(DateTime.now().toString(DATATIME_FORMAT_YYYYMMDDHHMMSSSSS));
	}

	public static String getCurTimes() {
		return DateTime.now().toString(DATATIME_FORMAT_YYYYMMDDHHMMSS);
	}

	public static String getCurDay() {
		return DateTime.now().toString(DATETIME);
	}

	public static String getCurDay(String format) {
		return DateTime.now().toString(format);
	}

	public static String getCurTime() {
		return DateTime.now().toString(TIME_HHMMSS);
	}

	public static String getCurrentTimeFormat() {
		return DateTime.now().toString(DATATIME_FORMAT);
	}

	/**
	 * 用SimpleDateFormat计算时间差
	 * @param registDate 字符串格式yyyyMMddHHmmssSSS
	 * @return
	 * @throws ParseException
	 */
	public static int calculateTime(String registDate) throws ParseException {
		SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		Date registDay = simpleFormat.parse(registDate);
		Date today = new Date();
		long oldDate = registDay.getTime();
		long nowDate = today.getTime();
		int days = (int) ((nowDate - oldDate) / (1000 * 60 * 60 * 24));
		return days;
	}

	/**
	 * 通过时间秒毫秒数判断两个时间的间隔的天数
	 * @param date1
	 * @param date2
	 * @return
	 * @throws ParseException
	 */
	public static int differentDays(String date1,String date2) throws ParseException
	{
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		Date d1 = df.parse(date1);
		Date d2 = df.parse(date2);
		long diff = d1.getTime() - d2.getTime();//得到的差值是微秒级别
		long days = diff / (1000 * 60 * 60 * 24);//转化为天数

		return (int) days;
	}


	/**
	 * 返回i天之后的日期 time 格式  yyyyMMdd
	 * @param time
	 * @param i
	 * @return
	 * @throws Exception
	 */
	public static String dateAdd(String time, int i) throws  Exception {
		SimpleDateFormat df = new SimpleDateFormat(DATEFORDAY);
		Calendar c = Calendar.getInstance();
		Date date = df.parse(time);
		c.setTime(date);
		c.add(Calendar.DATE,i);

		return df.format(c.getTime());
	}


	/**
	 * 取出一个指定长度大小的随机正整数.
	 *
	 * @param length int 设定所取出随机数的长度。length小于11
	 * @return int 返回生成的随机数。
	 */
	public static int getRandom(int length) {
		int num = 1;
		double random = Math.random();
		if (random < 0.1) {
			random = random + 0.1;
		}
		for (int i = 0; i < length; i++) {
			num = num * 10;
		}
		return (int) ((random * num));
	}

}
